Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: load static assets manually so all other requests rely on wasm_handler #226

Closed
wants to merge 1 commit into from

Conversation

Angelmmiguel
Copy link
Contributor

This is the first PR to complete #175. Before this PR, the default service that captured any non-processed request was the actix-files service (via Files). Since we wanted to mount static files into /, Files was capturing al /.* routes. For workers, wws was mounting their routes explicitly.

In #175, our intention is the opposite. We want workers_handler to manage any unknown route, so we can add new routes when the server is running. My first approach was to find a way to pass a request to worker_handler from Files using the default_handler. The Files service triggers this handler when it doesn't find any matching file. However, the types didn't match and it was an extra step before processing the request.

For this reason, I changed the approach and set all the static assets routes manually. Then, wws sends any unknown route to the wasm_handler to detect if there's any worker that manages it. In fact, this is a more correct approach as static files have a higher priority than workers (like dynamic).

@Angelmmiguel Angelmmiguel added the 🚀 enhancement New feature or request label Oct 2, 2023
@Angelmmiguel Angelmmiguel added this to the v1.6.0 milestone Oct 2, 2023
@Angelmmiguel Angelmmiguel requested a review from a team October 2, 2023 08:16
@Angelmmiguel Angelmmiguel self-assigned this Oct 2, 2023
@@ -25,7 +25,7 @@ pub async fn handle_assets(req: &HttpRequest) -> Result<NamedFile, Error> {
// Same as before, but the file is located at ./about.html
let html_ext_path = root_path.join(format!("public{uri_path}.html"));

if file_path.exists() {
if file_path.exists() && !uri_path.is_empty() && uri_path != "/" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need this new condition to avoid returning the public folder information.

Comment on lines -45 to -53
if let Some(route) = selected_route {
// First, check if there's an existing static file. Static assets have more priority
// than dynamic routes. However, I cannot set the static assets as the first service
// as it's captures everything.
if route.is_dynamic() {
if let Ok(existing_file) = handle_assets(&req).await {
return existing_file.into_response(&req);
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're explicitly mounting the static files in certain routes, this is not required anymore.

Comment on lines +82 to +106
let workers = WORKERS
.read()
.expect("error locking worker lock for reading");

// Configure the KV store when required
for route in serve_options.base_routes.routes.iter() {
let worker = workers
.get(&route.worker)
.expect("unexpected missing worker");

// Configure KV
if let Some(namespace) = worker.config.data_kv_namespace() {
data.kv.create_store(&namespace);
}
}

// Pre-create the KV namespaces
let data_connectors = Data::new(RwLock::new(data));

// Static assets
let mut static_assets =
StaticAssets::new(&serve_options.root_path, &serve_options.base_routes.prefix);
static_assets
.load()
.expect("Error loading the static assets");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I extracted all the initialization befure the HttpServer::new call. Actix runs this block on every thread, so we avoid calculating static assets multiple times.

@Angelmmiguel
Copy link
Contributor Author

@ereslibre don't review this yet, as I need to rethink about this issue. I may close this PR and open a more well-defined issue with a proper set of goals and definition

@Angelmmiguel Angelmmiguel removed this from the v1.6.0 milestone Oct 3, 2023
@Angelmmiguel
Copy link
Contributor Author

For now, I'm going to close this PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants